package top.chatgqt.SmartChargeStation.demo.websocket.spring.handler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.WebSocketMessage;
import org.springframework.web.socket.WebSocketSession;

/**
 * 自定义处理器
 */
@Component
@Slf4j
public class ImoocHandler implements WebSocketHandler {
    // 握手之后触发
    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        log.info(">>>>> 基于Spring注解 连接建立成功 <<<<<");
    }

    // 消息业务逻辑处理
    @Override
    public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {

        String payload = message.getPayload().toString();

        Object token = session.getAttributes().get("token");

        log.info("payload: {}",payload);
        log.info("token: {}",token);
    }

    // 出现错误触发
    @Override
    public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {

    }

    // 断开连接触发
    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception {

    }

    // 是否支持内容切片处理
    @Override
    public boolean supportsPartialMessages() {
        return false;
    }
}
